<template>
	<view class="container">
		<view class="bgImgBox">
			<image src="../../static/detail/productListBg.jpg" mode=""></image>
		</view>
		<view v-if="!leftMsg || !leftMsg.length">
			<u-loading-page :loading="!leftMsg || !leftMsg.length" loading-text="加载中..."></u-loading-page>
		</view>
		<view class="content" v-else>
			<view class="leftBox">
				<view :class="item.checked?'isChecked leftBoxItem':'leftBoxItem'" v-for="(item,index) in leftMsg" :key="index"
					@click="onLeftBoxClick(item)">
					<text class="greenLine"></text>
					<text class="leftTitleBox">{{item.typeName}}</text>
					<text class="leftTitleNewNumBox" v-if="item.newNum">{{item.newNum}}</text>
				</view>
			</view>
			<view class="rightBox">
				<view v-for="(item,index) in rightMsg" :key="index" @click="onItemClick(item)">
					<view class="title">
						<view>
							{{item.name}}
						</view>
						<text class="rightNewBox" v-if="item.newPro">新</text>
						<text class="rightNewBox" v-if="item.hotFlag">热</text>
					</view>
					<text>{{item.nameIntroduction || '点击查看详情'}}</text>
				</view>
				<text v-if="!rightMsg.length" class="noMsg">
					暂无数据
				</text>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		queryAllTypeAndNameList,
		queryByCondition
	} from "@/apis/product.js"
	import noMsgBox from "@/components/noMsgBox.vue"
	
	export default {
		components:{
			noMsgBox
		},
		data() {
			return {
				leftMsg: [],
				rightMsg: [],
				checked: null
			};
		},
		onLoad() {
			this.getList()
		},
		onShow() {
			this.updateList()
		},
		methods: {
			getList(target) {
				queryAllTypeAndNameList().then(res => {
					if (res.code == '00000000') {
						for (let item of res.data) {
							item.checked = false
						}
						if(target && target != -1 && Array.isArray(res.data)){
							target = this.checked
							let isSave = res.data.find(iterator=>{
								return iterator.type == target.type && target.typeName == iterator.typeName
							})
							if(isSave && isSave != -1){
								for (const iterator of res.data) {
									if(iterator.type == target.type && target.typeName == iterator.typeName){
										iterator.checked = true;
										this.rightMsg = iterator.productionVOList
									}
								}
							} else {
								res.data[0].checked = true
								this.rightMsg = res.data[0].productionVOList
							}
						}else{
							res.data[0].checked = true
							this.rightMsg = res.data[0].productionVOList
						}
						this.leftMsg = res.data
					}
				})
			},
			updateList(){
				if(Array.isArray(this.leftMsg) && this.leftMsg.length > 0){
					let isChecked = this.leftMsg.some(item=>{
						return item.checked
					})
					if(isChecked){
						let item = this.leftMsg.find(item=>{
							return item.checked
						})
						item = this.checked?.typeName == (item&&item.typeName) ? item : this.checked
						this.getList(item)
					}else{
						this.getList()
					}
				}
			},
			onLeftBoxClick(val) {
				for (let i in this.leftMsg) {
					this.leftMsg[i].checked = false
				}
				val.checked = true
				this.checked = val
				this.rightMsg = val.productionVOList || []
			},
			onItemClick(item) {
				if(!uni.getStorageSync('token')){
					uni.setStorageSync('needLogin',true)
					uni.switchTab({
						url:'/pages/index/index'
					})
					return
				}
				uni.navigateTo({
					url: '/pages/product/productDetail?id=' + item.id
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		height: 100vh;

		.content {
			display: flex;
			justify-content: space-between;
			height: calc(100% - 162rpx);
		}

		.bgImgBox {
			width: 100%;
			height: 162rpx;

			image {
				height: 100%;
				width: 100%;
			}
		}

		.leftBox {
			width: 218rpx;
			height: 100%;
			overflow-y: auto;
			background-color: #fff;

			&>view {
				height: 120rpx;
				width: 100%;
				text-align: center;
				line-height: 120rpx;
				font-size: 30rpx;
			}

			.isChecked {
				background-color: rgb(240, 251, 245);
				color: #00BF5B;

				.greenLine {
					height: 60rpx;
					width: 6rpx;
					position: absolute;
					left: 10rpx;
					top: 30rpx;
					border-radius: 5rpx;
					background-color: #00BF5B;
				}
			}
			.leftBoxItem{
				position: relative;
			}

			.leftTitleBox {
				height: 100%;
				width: 80%;
				margin: 0 auto;
				text-overflow: ellipsis;
				white-space: nowrap;
				box-sizing: border-box;
				overflow: hidden;
				display: block;
			}

			.leftTitleNewNumBox {
				position: absolute;
				right: 26rpx;
				top: 22rpx;
				font-size: 20rpx;
				border-radius: 40rpx;
				background-color: #F56C6C;
				padding: 8rpx 12rpx;
				color: #fff;
				min-height: 16rpx;
				line-height: 16rpx;
			}

		}

		.rightBox {
			width: calc(100% - 218rpx);
			background-color: #FAFAFA;
			padding: 30rpx 30rpx 10rpx;
			height: 100%;
			overflow-y: auto;
			box-sizing: border-box;

			&>view {
				background-color: #fff;
				padding: 30rpx;
				margin-bottom: 20rpx;
				border-radius: 12rpx;

				.title {
					display: flex;
					&>view {
						color: #000000;
						font-size: 28rpx;
						font-weight: 600;
						text-overflow: ellipsis;
						white-space: nowrap;
						box-sizing: border-box;
						overflow: hidden;
					}

					.rightNewBox {
						font-size: 18rpx;
						font-weight: 400;
						letter-spacing: 0px;
						margin-left: 16rpx;
						padding: 4rpx;
						color: rgba(255, 97, 97, 1);
						border: 1rpx solid #FF6161;
						border-radius: 6rpx;
						vertical-align: top;
					}
				}

				&>text {
					font-size: 26rpx;
					color: #9C9C9C;
					line-height: 36rpx;
					// height: 74rpx;
					margin-top: 10rpx;
					display: block;
					overflow: hidden;
					-webkit-line-clamp: 2;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
				}
			}

			.noMsg {
				font-size: 26rpx;
			}
		}
	}
</style>
